<?php
/*
 * Cache Class
 * 
 * Performs Caching, can cache pages that will almost never change
 * 
 * @author : Jason Meulenhoff
 * @mail : mail@kacangboi.com
 * @web : http://kacangboi.com
 * @copyright : KacangNetworks 2010
 * 
 */
class cache 
{
	private $cache;
	private $buffer;
	
	public function __construct($cache)
	{
		$this->cache = $cache;
		debug::message("Caching is enabled, trying to determine if file $this->cache exist","INFO");
		if(!file_exists($this->cache))
		{	
			debug::message("Cache file $this->cache does not exists creating one","INFO");
			fopen($this->cache,"w");
		}
		debug::message("Cache file $this->cache exists","INFO");
	}
	public function writeCache($buffer)
	{
		debug::message("Writing to cache file $this->cache","INFO");
		fwrite(fopen($this->cache,"w"),serialize($buffer));	
	}
	public function prepareCache()
	{
		debug::message("Preparing to cache output","INFO");
		ob_start();
	}
	public function endCache()
	{
		debug::message("Finalizing Cache File $this->cache, saving the output","INFO");
		$this->buffer = ob_get_contents();
		ob_end_clean();
		$this->writeCache($this->buffer);
	}
	public function readBuffer()
	{
		debug::message("Reading contents from the buffer","INFO");
		echo $this->buffer;
	}
	public function readCache()
	{
		debug::message("Reading contents from Cache $this->cache","INFO");
		echo unserialize(file_get_contents($this->cache));
	}
	public function cacheDate()
	{
		debug::message("Cache File $this->cache Last Modified at ".filemtime($this->cache)." ","INFO");
		return filemtime($this->cache);
	}
	public function __destruct()
	{
		fclose($this->cache);
	}
	public function oldCacheFile()
	{
		debug::message("Checking if cache file $this->cache is old","INFO");
		if ($this->cacheDate() >( time() - 60 ))
		{
			debug::message("Cache $this->cache is still up to date","INFO");
			return false;
		}else
		{
			debug::message("Cache $this->cache needs to be updated","INFO");
			return true;
		}		
	}
}
?>